package com.xiangxiangjd.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.xiangxiangjd.analyse.bean.RepairMethod;
import com.xiangxiangjd.analyse.controller.helper.$;
import com.xiangxiangjd.analyse.utils.JsonUtils;
import com.xiangxiangjd.analyse.utils.excel.ExportExcel;
import com.xiangxiangjd.analyse.utils.excel.ImportExcel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xiangxiangjd.analyse.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.analyse.service.RepairService;
import com.xiangxiangjd.analyse.bean.Repair;
import com.xiangxiangjd.analyse.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-03-18
 */
@Controller
@RequestMapping("/analyse/repair")
public class RepairController extends BaseController {


    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main(Model model) {

        QueryWrapper<Repair> queryWrapper = new QueryWrapper();

        queryWrapper.select("DISTINCT item").orderByDesc("update_time");

        List<Repair> list = repairService.list(queryWrapper);
        List<String> data = list.stream().map(Repair::getItem).collect(Collectors.toList());

        System.out.println("data:====================="+data.toString());



        model.addAttribute("items", data); //下拉所有的修复项目


        return "Repair/RepairMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addRepairHtml")
    public String addRepairHtml() {
        return "Repair/RepairAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editRepairHtml")
    public String editRepairHtml(String id, Model model) {
        model.addAttribute("repair", repairService.selectRepairByIdRepair(id));
        return "Repair/RepairEdit";
    }

    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showRepairHtml")
    public String showRepairHtml(String id, Model model) {
        Repair repair = repairService.selectRepairByIdRepair(id);

        repair.setUpdateTime(StringToDate(repair.getUpdateTime()));
        repair.setCreateTime(StringToDate(repair.getCreateTime()));

        System.out.println("repair:======================"+repair.toString());
        model.addAttribute("repair", repair);



        return "Repair/RepairShow";
    }


    /**
     * 分页查询Repair
     *
     * @param page   页码
     * @param limit  查几条
     * @param repair 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findRepairByPage")
    @ResponseBody
    public ResultPage findRepairByPage(Integer page, Integer limit, Repair repair) {
        return repairService.findRepairByPage(page, limit, repair);
    }

    /**
     * 添加Repair
     *
     * @param repair 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addRepair")
    @ResponseBody
    public ApiResultModel saveRepair(Repair repair) {
        return repairService.addRepair(repair);
    }

    /**
     * 编辑Repair
     *
     * @param repair 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editRepair")
    @ResponseBody
    public ApiResultModel editRepair(Repair repair) {
        return repairService.editRepair(repair);
    }


    /**
     * 删除Repair
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delRepair", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delRepair(@RequestParam("datas[]") ArrayList ids) {


        return repairService.delRepairs(ids);
    }

    /**
     * 下载导入的ex 的表格
     */
    @ResponseBody
    @RequestMapping(value = "get/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {

        String fileName = "飞机修理任务级别分析统计系统.xlsx";
        List<Repair> list = Lists.newArrayList();

        new ExportExcel("飞机修理任务级别分析统计系统", RepairMethod.class, 1).setDataList(list).write(response, fileName).dispose();


    }


    /**
     * 导入Excel   批量导入数据
     *
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "Import")
    public ApiResultModel importFile(@RequestParam("file") MultipartFile file) {

        try {

            ImportExcel ei = new ImportExcel(file, 1, 0);


            List<Repair> list = ei.getDataList(Repair.class);


            //批量插入
            repairService.saveBatch(list);


            // 导入成功
            return $.success();

        } catch (IOException e) {

        } catch (InvalidFormatException e) {

        } catch (IllegalAccessException e) {

        } catch (InstantiationException e) {

        }

        return $.error();
    }

    /**
     * 获取所有的修理项目
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get/repairProject")
    public ApiResultModel repairProject() {


        QueryWrapper<Repair> queryWrapper = new QueryWrapper();


        queryWrapper.select("DISTINCT item").orderByDesc("update_time");

        List<Repair> list = repairService.list(queryWrapper);
        List<String> data = list.stream().map(Repair::getItem).collect(Collectors.toList());

        return $.success(data);
    }


}
